﻿
/// <reference path="shell.app.js" />
$$.DB = new function () {
    var me = this;
    var memory = new $$.DBCache();
    Object.defineProperty(me, "Ver", { value: 1180611 });
    Object.defineProperty(me, "Never", { value: -999 });

    //minute：单位分钟，默认600

    me.SetLocal = function (tb, key, row, minute) {
    	if (memory.Set(tb, key, row, minute) === undefined) return;
        if (!window.$$Shell || !$$Shell.SetRow) return;
        if (!minute) minute = 0;
        $$.Shell._exec("SetRow", { tb: tb + "", id: key + "", val: row, minute: minute });
    };
    me.Set = me.SetLocal;
    me.SetCache = memory.Set;

    me.SetLocals = function (tb, keys, rows, minute) {
    	if (memory.Sets(tb, keys, rows, minute) === undefined) return;
    	if (!window.$$Shell || !$$Shell.SetRows) return;
    	if (!minute) minute = 0;
    	$$.Shell._exec("SetRows", { tb: tb + "", ids: keys, vals: rows, minute: minute });
    };
    me.Sets = me.SetLocals;
    me.SetCaches = memory.Sets;

    me.GetLocal = function (tb, key, callback) {
        if (!callback) { console.error("db Get/GetLocal no callback:" + tb); return; }
    	if (!window.$$Shell || !$$Shell.GetRow) { callback(); return; }
        var guid = $$.Shell._addCallback(function (row) {
            if (row || row === 0 || row === false) memory.Set(tb, key, row, 15);
            callback(row);
        });
        $$.Shell._exec("GetRow", { tb: tb + "", id: key + "" }, guid);
    };
    me.Get = function (tb, key, callback) {
        if (!callback) { console.error("db Get/GetLocal no callback:" + tb); return; }
        var row = memory.Get(tb, key);
        if (row === undefined) { callback(); return; }
        if (row || row === 0 || row === false) { callback(row); return; }
        me.GetLocal(tb, key, callback);
    };
    me.GetCache = memory.Get;

    me.GetLocals = function (tb, keys, callback) {
    	if (!callback) { console.error("db Gets/GetLocals no callback:" + tb); return; }
        if (!window.$$Shell || !$$Shell.GetRows) { callback(null); return; }

        var guid = $$.Shell._addCallback(function (rows) {
            if (!rows || rows.length < 1) { callback(null); return; }
            //rows.uns = [];
            memory.Sets(tb, keys, rows, 0);
            callback(rows);
        });
        $$.Shell._exec("GetRows", { tb: tb + "", ids: ids }, guid);
    };
    me.Gets = function (tb, keys, callback) {
        if (!callback) { console.error("db Gets/GetLocals no callback:" + tb); return; }
        var rows = memory.Gets(tb, keys);
        if (rows === undefined) { callback(undefined); return; }
        if (rows && rows.uns && rows.uns.length < 1) { callback(rows); return; }
        if (!window.$$Shell || !$$Shell.GetRows) { callback(null); return; }

        if (!rows) rows = [];
        var uns = rows.uns, ids = [];
        if (!uns) {
            uns = []; ids = keys;
            for (var i = 0, len = keys.length; i < len; i++) {
                uns.push(i);
            }
        }
        else {
            for (var i = 0; i < uns.length; i++) {
                ids.push(keys[uns[i]]);
            }
        }

        var guid = $$.Shell._addCallback(function (ros) {
            if (!ros || ros.length < 1) { callback(rows); return; }
            rows.uns = [];
            var len = Math.min(ros.length, uns.length);
            memory.Sets(tb, ids, ros, 15);
            for (var i = 0; i < len; i++) {
                var row = ros[i], idx = uns[i];
                if (idx >= 0 && (row || row === 0 || row === false)) rows[idx] = row;
                else rows.uns.push(idx);
            }
            callback(rows);
        });
        $$.Shell._exec("GetRows", { tb: tb + "", ids: ids }, guid);
    };
    me.GetCaches = memory.Gets;

    me.Remove = function (tb, key) {
    	if (memory.Remove(tb, key) === undefined) return;
    	if (!window.$$Shell || !$$Shell.RemoveRow) return;
    	$$.Shell._exec("RemoveRow", { tb: tb + "", id: key + "" });
    };
    me.Removes = function (tb, keys) {
    	if (memory.Removes(tb, keys) === undefined) return;
    	if (!window.$$Shell || !$$Shell.RemoveRows) return;
    	$$.Shell._exec("RemoveRows", { tb: tb + "", ids: keys });
    };

    me.Clear = function(tb, iscache)
    {
    	//tb=** 清除所有表
    	if (memory.Clear(tb) === undefined) return;
    	if (iscache === true || !window.$$Shell || !$$Shell.ClearDB) return;
    	$$.Shell._exec("ClearDB", { tb: tb + "" });
    }

    function init() {
    	me.Get("app", "ver", function (ver) {
    		ver = Number(ver);
    		if (ver && ver === Ver) return;
    		if(ver) me.Clear("**");
    		setTimeout(function () { me.SetLocal("app", "ver", Ver, me.Never); }, 300);
    	});
    }
    init();
};